Setup and storing functions

knitr::opts_chunk$set(echo = TRUE, warning = FALSE, message = FALSE)
library(here)
library(tidyverse)
library(janitor)
library(broom)
library(equatiomatic)

options(scipen = 999)


electricity <- read_csv(here("HW3_data.csv")) %>% 
  clean_names() %>% 
  select(-1)

# running linear models to get demand curves for high and low income consumers
model_demand_l <- lm(price_cents  ~ q_low_kwh, data = electricity)
model_demand_h <- lm(price_cents ~ q_high_kwh, data = electricity)


## FUNCTIONS

# demand 
demand <- function(p, model){
  q <- (p - model$coefficients[[1]])/model$coefficients[[2]]
  q <- ifelse(q<0,0,q)
  return(q)
}

# aggregate demand
demand_agg <- function(p){
  q <- demand(p, model_demand_l) + demand(p, model_demand_h)
  return(q)
}

# consumer surplus
CS <- function(p, model){
  q <- demand(p, model)
  cs <- 0.5*(model$coefficients[[1]] - p)*q
  return(cs)
}

# aggregate consumer surplus
CS_agg <- function(p){
  cs <- CS(p,model_demand_l) + CS(p,model_demand_h)
  return(cs)
}

# marginal cost
mc_slope <- 10/demand_agg(10)
# marginal cost function
mc <- function(p){
  mc <- p*(mc_slope)
  return(mc)
}


# determining marginal external cost
metric_ton <- 2204.62
mec_cents <- (0.85/metric_ton)*100*51

Visualization

# creating a table for demand based on the linear model
price_vector <- c(0:35)
table <- data.frame(price_vector)
demand_table <- table %>% 
  mutate(demand_low = demand(price_vector, model = model_demand_l)) %>% 
  mutate(demand_high = demand(price_vector, model = model_demand_h)) %>% 
  mutate(demand_agg = demand_agg(price_vector)) %>% 
  rename(price_cents = price_vector) %>% 
  mutate(marginal_cost = mc(price_cents))

# aggregate demand curve
model_demand_agg <- lm(price_cents ~ demand_agg, data = demand_table)


demand_table_longer <- demand_table %>% 
  pivot_longer(2:5, names_to = "demand_level", values_to = "demand")

# plotting supply and demand
ggplot(data = demand_table_longer, aes(x = demand, y = price_cents, color = demand_level)) +
  geom_line(size = 1) +
  theme_minimal(14) +
  scale_color_manual(values = c("darkblue", "cyan4", "cadetblue3", "black", "firebrick")) +
  scale_x_continuous(limits = c(1, 9e+05)) +
  theme(legend.position = c(0.8, 0.7)) + 
  geom_line(data = demand_table_longer, aes(x = demand, y = mec_cents + mc_slope*demand),
            color = "forestgreen",
            size = 1) +
  geom_line(data = demand_table_longer, aes(x = demand, y = mec_cents),
            color = "firebrick",
            size = 1) +
  labs(x = "kWh electricity used", y= "Price (cents)") +
  theme(legend.title = element_blank())

1. Price per kwh of electricity

metric_ton <- 2204.62
price_per_kwh_cents<- (0.85/metric_ton)*100*51

Marginal external cost per kWh of electricity: 1.97 cents

2. Supply and demand curves, consumer/producer benefits

# storing slopes and intercepts of each equation to report inline code 
int_high <- model_demand_h$coefficients[1]
slope_high <- model_demand_h$coefficients[2]
int_low <- model_demand_l$coefficients[1]
slope_low <- model_demand_l$coefficients[2]

# calculating surpluses and environmental costs
cs2 <- CS_agg(10) # consumer surplus
ps2 <- 10*demand_agg(10)/2 # producer surplus
enviro_cost_3 <- mec_cents*demand_agg(10) # environmental cost

Demand curve for high income consumers: P = 31.61 -0.000052Q

Demand curve for low income consumers: P = 23.37 -0.00011Q

Aggregate demand curve: To find the aggregate demand curve, horizontally sum the two demand curves above. FIND ANSWER TO THIS

Supply curve: P = 0.000019Q

Consumer benefit: $52987.22

Producer benefit: $26835.97

Environmental Cost: $10553.65

3. Consumer benefit by population

# finding the consumer surplus for each income group
cs_low3 <- CS(10, model_demand_l) # consum
cs_high3 <- CS(10, model = model_demand_h)

Consumer benefit for high income consumers: $44874.79

Consumer benefit for low income consumers: $8112.43

4. Optimal electricity tax

Optimal Electricity Tax: 1.97 cents

A. The amount of electricity produced and consumed: B. The price of electricity C. Overall welfare of “high” income consumers D. Overall welfare of “low” income consumers E. Power suppliers (i.e., electricity producers) F. Total environmental damage G. Total tax revenue generated

5. Tax redistribution

Make a table for this that includes: (a) Overall welfare of “high” income consumers (b) Overall welfare of “low” income consumers (c) Electricity producers

6. Solar generation

  1. What is total electricity consumption?
  2. What is the total environmental externality?
  3. What value of the electricity tax makes the total environmental damage the same as the damage when solar panels are available to the high income group?